package com.egg.ylt.adapter.record;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.GlideUtils;
import com.egg.ylt.layoutmanager.WrapContentGridLayoutManager;
import com.egg.ylt.pojo.BabyDynamicMsgEntity;
import com.egg.ylt.presenter.impl.RecordPresenter;
import com.egg.ylt.widget.imagepicker.ImagesDataHolder;
import com.yonyou.framework.library.adapter.rv.CommonAdapter;
import com.yonyou.framework.library.adapter.rv.ItemViewDelegate;
import com.yonyou.framework.library.adapter.rv.ViewHolder;
import com.yonyou.framework.library.common.utils.DimenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By Dlx
 * on 2019/1/19
 */
public class RecordMulitPicViewType implements ItemViewDelegate<BabyDynamicMsgEntity.ListBean> {
    private RecordPresenter mPresenter;
    private RecyclerView recyclerView;

    public RecordMulitPicViewType(RecordPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.item_record_mulitpic;
    }

    @Override
    public boolean isForViewType(BabyDynamicMsgEntity.ListBean item, int position) {
        if ("2".equals(item.getRecordType()) && item.getImageUrlList() != null && item.getImageUrlList().size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void convert(ViewHolder holder, final BabyDynamicMsgEntity.ListBean listBean, final int position) {
        holder.setText(R.id.item_record_singlepic_relationship_tv, listBean.getRelationName());
        holder.setText(R.id.item_record_singlepic_date_tv, listBean.getTime());
        holder.setText(R.id.record_date_textview, listBean.getDate());
        holder.setText(R.id.record_date_days_textview, listBean.getWeek());
        holder.setText(R.id.record_content_tv, listBean.getContent());
        recyclerView = holder.getView(R.id.record_pics_rv);
        if (listBean.getImageUrlList() != null) {
            int size = listBean.getImageUrlList().size();
            int type = 1;
            if (size == 1) {
                loadSinglePic(holder, listBean, position, type);
            } else if (size > 1 && size <= 4) {
                type = 2;
                loadSinglePic(holder, listBean, position, type);
            } else if (size > 4 && size <= 9) {
                type = 3;
                loadSinglePic(holder, listBean, position, type);
            }

        } else {
            recyclerView.setVisibility(View.GONE);
        }
        holder.setOnClickListener(R.id.item_record_singlepic_deleteiv, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.deleteBabyDynamicMsg( listBean.getId(),0, position);
            }
        });
    }

    private void loadSinglePic(ViewHolder holder, final BabyDynamicMsgEntity.ListBean listBean, final int position, final int type) {
        recyclerView.setVisibility(View.VISIBLE);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mPresenter.getContext(), type);
        gridLayoutManager.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(gridLayoutManager);

        final List<String> list = listBean.getImageUrlList();
        CommonAdapter<String> adapter = new CommonAdapter<String>(mPresenter.getContext()) {
            @Override
            protected int itemLayoutId() {
                int layoutId = -1;
                switch (type) {
                    case 1:
                        layoutId = R.layout.item_pic;
                        break;
                    case 2:
                        layoutId = R.layout.item_pic_h86dp;
                        break;
                    case 3:
                        layoutId = R.layout.item_pic_h54dp;
                        break;
                }
                return layoutId;
            }

            @Override
            protected void convert(ViewHolder holder, final String s, final int position) {
                Glide.with(mPresenter.getContext()).load(s).placeholder(R.drawable.icon_picloading).into((ImageView) holder.getView(R.id.item_pic));
                holder.setOnClickListener(R.id.item_pic, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImagesDataHolder.getInstance().holdeImageUrlList(listBean.getImageUrlList());
                        mPresenter.jumpToMultiPics(position);
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
        adapter.update(list);
    }
}
